using System;
using System.Collections.Generic;
using Mahjong.UI;

namespace Mahjong.UI
{
//player从1开始， 
public class Player
{
    private PlayerUI ui;
    private Manager manager;
    //存储玩家UI对象字典
    private Dictionary<int, PlayerUI> playerObjectsUI = new Dictionary<int, PlayerUI>();
    private Dictionary<int, Player> playerObjects = new Dictionary<int, Player>();
    //存储玩家手牌信息
    private Dictionary<int, List<int>> playerHandCards = new Dictionary<int, List<int>>();
    //存储玩家弃牌信息
    private Dictionary<int, List<int>> playerDiscardCards = new Dictionary<int, List<int>>();


    public void CreatePlayerObjects(int playerCount)
    {//创建玩家对象和手牌、弃牌区对象
        for (int i = 0; i < playerCount; i++)
        {
            playerHandCards[i] = new List<int>();
            playerDiscardCards[i] = new List<int>();
            playerObjects[i] = null;
        }
    }
    //设置playerUI引用的方法
    public void SetPlayerUI(int playerId, PlayerUI ui)
    {
        // 确保字典中存在该玩家的条目
        if (!playerObjects.ContainsKey(playerId))
        {
            playerObjects[playerId] = null;
        }
        // 设置PlayerUI引用
        playerObjectsUI[playerId] = ui;
    }
    // 获取playerUI引用的方法
    public PlayerUI GetPlayerUI(int playerId)
    {
        return playerObjectsUI.ContainsKey(playerId) ? playerObjectsUI[playerId] : null;
    }

    //根据玩家ID获取玩家对象
    public Player GetPlayer(int playerId)
    {
        if (playerObjects.ContainsKey(playerId))
        {
            return playerObjects[playerId];
        }
        return null;
    }

    //获取playerUI中的手牌举起状态并传递
    public bool IsCardRaised(int playerId)
    {
        PlayerUI ui = GetPlayerUI(playerId);
        if (ui != null)
        {
            return ui.IsCardRaised();
        }
        return false;
    }

    //展示吃牌时的复杂选项
    public void ShowChiToUi(int playerId,List<List<int>> options, Action<List<int>> callback)
    {
        PlayerUI ui=GetPlayerUI(playerId);
        if (ui != null)
        {
             ui.ShowChiToSelector(options, callback);
            // 实际调用UI的显示逻辑
                /*添加确认玩家点击状态的逻辑*/
        }
    }
    //展示所有的可操作选项
     public void GetResponseOptions(int playerId, List<string> availableOperations, Action<string> callback)
    {
          // 获取当前玩家ID
        int currentPlayerId = GetCurrentPlayerId();

        // 创建一个字典来映射操作类型到对应的处理逻辑
        Dictionary<string, Action<List<List<int>>, Action<List<int>>> operationHandlers = new Dictionary<string, Action<List<List<int>>, Action<List<int>>>>
        {
            { "chi", ShowChiSelection },
            { "peng", ShowPengSelection },
            { "gang", ShowGangSelection },
            { "hu", ShowHuSelection }
        };

        // 遍历可用操作列表
        foreach (var operation in availableOperations)
        {
            if (operationHandlers.ContainsKey(operation))
            {
                // 获取对应的操作处理逻辑
                Action<List<List<int>>, Action<List<int>>> handler = operationHandlers[operation];

                // 创建一个包装回调函数，用于处理玩家选择
                Action<List<int>> wrappedCallback = selected =>
                {
                    if (selected == null || selected.Count == 0)
                    {
                        Console.WriteLine("玩家取消选择或选择为空");
                    }
                    // 调用传入的回调函数，传递操作类型
                    callback(operation);
                };

                // 调用对应的操作处理逻辑
                // 这里假设每个操作都需要传入 options 和 callback
                // 实际情况下，可能需要根据具体操作调整参数
                List<List<int>> options = GetOperationOptions(operation, currentPlayerId);
                handler(options, wrappedCallback);
            }
        }
    }
    //获取对应操作的选项
    public List<List<int>> GetOperationOptions(string operation, int playerId)
    {
        switch (operation.ToLower())
        {
            case "chi":
                // 返回吃牌选项
                return logic.GetChiOptions(playerId);
            case "peng":
                // 返回碰牌选项
                return logic.GetPengOptions(playerId);
            case "gang":
                // 返回杠牌选项
                return logic.GetGangOptions(playerId);
            case "hu":
                // 返回胡牌选项
                return logic.GetHuOptions(playerId);
            default:
                return new List<List<int>>();
        }
    }
       
    }
    //展示碰牌时的选项
    public void ShowPengToUi(int playerId, List<List<int>> options, Action<List<int>> callback)
    {
        if (ui != null)
        {
             ui.ShowPengToSelector(options, callback);
            // 实际调用UI的显示逻辑
                /*添加确认玩家点击状态的逻辑*/
           
        }
    }
    
    //展示杠牌时的复杂选项
    public void ShowGangToUi(int playerId, List<List<int>> options, Action<List<int>> callback)
    {
        if (ui != null)
        {
             ui.ShowGangToSelector(options, callback);
            // 实际调用UI的显示逻辑
                /*添加确认玩家点击状态的逻辑*/
           
        }
    }
     //展示胡牌时的复杂选项
    public void ShowHuToUi(int playerId, List<List<int>> options, Action<List<int>> callback)
    {
        if (ui != null)
        {
             ui.ShowHuToSelector(options, callback);
            // 实际调用UI的显示逻辑
                /*添加确认玩家点击状态的逻辑*/
           
        }
    }


    /*单独设计button作为入口，确定玩家想要吃、碰、杠还是胡或者是过*/

    public void DistributeCards(int playerId, List<int> cards)
    {//发牌操作
        if (playerHandCards.ContainsKey(playerId))
        {
            playerHandCards[playerId] = cards;
        }
    }

    public void UpdateHandcard(int playerId, List<int> handCard)
    {//更新手牌
        if (playerHandCards.ContainsKey(playerId))
        {
            playerHandCards[playerId] = handCard;
        }
    }

    public void UpdateDiscard(int playerId, List<int> discardCard)
    {//更新弃牌
        if (playerDiscardCards.ContainsKey(playerId))
        {
            playerDiscardCards[playerId] = discardCard;
        }
    }
    //获取手牌信息
    public List<int> GetHandCard(int playerId)
    {
        return playerHandCards.ContainsKey(playerId) ? playerHandCards[playerId] : new List<int>();
    }

    //获取弃牌信息
     public List<int> GetDiscardCard(int playerId)
     {
        return playerDiscardCards.ContainsKey(playerId) ? playerDiscardCards[playerId] : new List<int>();
     }
}
}